<template>
  <div class="dashboard-container">
    <div class="app-container">
      <h2>员工</h2>
      <page-tools>
        <!-- 左侧结构 -->
        <template #slot-left>
          <span>共{{ total }}条信息</span>
        </template>
        <!-- 右侧结构 -->
        <template #slot-right>
          <el-button type="danger" size="small">导入excel</el-button>
          <el-button type="success" size="small">导入excel</el-button>
          <el-button type="primary" size="small">新增员工</el-button>
        </template>
      </page-tools>
      <!-- 主体部分 -->
      <el-card style="margin-top: 10px">
        <el-table
          :data="employeesList"
          border
          :default-sort="{prop:'workNumber'}"
        >
          <el-table-column type="index" label="序号" />
          <el-table-column prop="username" label="姓名" />
          <el-table-column label="头像">头像</el-table-column>
          <el-table-column prop="mobile" label="手机号" sortable />
          <el-table-column prop="workNumber" label="工号" />
          <el-table-column prop="formOfEmployment" label="聘用形式">
            <template slot-scope="scope">
              形式：{{ scope.row.formOfEmployment | formatEmployment }}
            </template>
          </el-table-column>
          <el-table-column prop="departmentName" label="部门" />
          <el-table-column prop="correctionTime" label="入职时间" sortable />
          <el-table-column prop="formOfEmployment" label="账户状态">
            <template>
              <el-switch
                active-color="pink"
                inactive-color="#ff4949"
              />
            </template>
          </el-table-column>
          <el-table-column label="操作" width="280">
            <template slot-scope="scope">
              <el-button type="danger" size="small">查看</el-button>
              <el-button type="success" size="small">分配角色</el-button>
              <el-button type="primary" size="small" @click="hDel(scope.row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页组件 -->
        <el-row
          type="flex"
          justify="center"
          align="middle"
          style="height: 60px"
        >
          <!-- 分页区域 -->
          <el-pagination
            :current-page="query.page"
            :page-sizes="[5,10, 15, 20, 25]"
            :page-size="query.size"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          /> </el-row></el-card>
    </div>
  </div>
</template>

<script>
import { getEmployeeList, delEmployee } from '@/api/employees'
import EmployeeEnum from '@/constant/employees.js'
console.log(EmployeeEnum.hireType)
// import pageTools from '@/components/PageTools/index.vue'
export default {
  name: 'Employees',
  //   过滤器
  // 作用：做格式
  filters: {
    // 定义
    // val要做格式转换的值
    formatEmployment(val) {
      console.log('formofEmployment', val)
      // 1：正式，2：非正式这个对应关系，不应该写死！
      console.log(obj)
      // 在EmployeeEnum.hireType数组中，找到id为val的元素，并返回它的value
      const obj = EmployeeEnum.hireType.find(item => item.id === val)
      if (obj) {
        return obj.value
      } else {
        return '未知'
      }
    }
  },
  data() {
    return {
      query: {
        page: 1, // 页码
        size: 10 // 页码条数
      },
      employeesList: [], // 员工列表
      total: 0 // 数据总条数
    }
  },
  created() {
    this.getEmployeeList()
  },
  methods: {
    async doDel(id) {
      const res = await delEmployee(id)
      console.log(res)
      //   删除成功
      // 如果当前的表格中只有一条数据，删除之后，要回到第一页
      if (this.employeesList.length === 1) {
        this.query.page--
        if (this.query.page <= 0) {
          this.query.page = 1
        }
      }
      // 1. 刷新页面
      this.getEmployeeList()
      // 2. 提示
      this.$message.success('删除成功')
    },
    // 用户点击了删除
    hDel(id) {
      this.$confirm('此操作将永久删除员工, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        // 单独封装一个方法
        this.doDel(id)
      }).catch(() => {

      })
    },
    async getEmployeeList() {
      const res = await getEmployeeList(this.query)
      this.employeesList = res.data.rows
      this.total = res.data.total
    },
    // 每页显示的条数发生改变时触发
    handleSizeChange(newSize) {
      this.query.size = newSize
      this.getEmployeeList()
    },

    // 当前页面发生改变时触发
    handleCurrentChange(newPage) {
      this.query.page = newPage
      this.getEmployeeList()
    }
  }

  //   components: {
  //     pageTools
  //   }
}
</script>

<style lang="scss" scoped></style>
